﻿namespace Flash.Web.UI
{
    using System;
    using System.Collections.Generic;
    using System.Web.Caching;

    public class Circumscription
    {
        private List<string> _cacheCircumscription = null;
        private List<string> _circumscription = null;
        private UserControl _control;
        private bool _isCreating = false;

        public Circumscription(UserControl control)
        {
            this._control = control;
        }

        public void BeginRecord()
        {
            if (this._control.Cache.Get(this._control.AppRelativeVirtualPath) == null)
            {
                this._isCreating = true;
                this._circumscription = new List<string>();
            }
        }

        public bool CheckValueName(string key)
        {
            return (this._isCreating || this.GetCircumscription().Contains(key));
        }

        public void EndRecord()
        {
            if (this._isCreating)
            {
                this._control.Cache.Insert(this._control.AppRelativeVirtualPath, this._circumscription, new CacheDependency(this._control.Server.MapPath(this._control.AppRelativeTemplateSourceDirectory)));
                this._isCreating = false;
                this._circumscription = null;
            }
        }

        private List<string> GetCircumscription()
        {
            if (this._cacheCircumscription == null)
            {
                object obj2 = this._control.Cache.Get(this._control.AppRelativeVirtualPath);
                if (obj2 != null)
                {
                    this._cacheCircumscription = (List<string>) obj2;
                }
                else
                {
                    this._cacheCircumscription = new List<string>();
                }
                return this._cacheCircumscription;
            }
            return this._cacheCircumscription;
        }

        public void RecordValueName(string key)
        {
            if (!this._circumscription.Contains(key))
            {
                this._circumscription.Add(key);
            }
        }

        public bool IsRecording
        {
            get
            {
                return this._isCreating;
            }
        }
    }
}

